package com.chx.service.Impl;

import com.chx.constant.StatusConstant;
import com.chx.context.BaseContext;
import com.chx.dto.*;
import com.chx.entity.SaleOrderDetails;
import com.chx.entity.SaleOrders;
import com.chx.entity.StockOutOrderDetails;
import com.chx.entity.StockOutOrders;
import com.chx.mapper.*;
import com.chx.result.PageResult;
import com.chx.service.SaleOrderService;
import com.chx.vo.SaleOrderDetailVo;
import com.chx.vo.SaleOrderVo;
import com.chx.wrapper.SaleOrderWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 */
@Service
public class SaleOrderServiceImpl implements SaleOrderService {
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StockOutOrderMapper stockOutOrderMapper;
    @Autowired
    private StockOutOrderDetailMapper stockOutOrderDetailMapper;
    @Autowired
    private ProductMapper productMapper;

    @Transactional
    @Override
    public void save(SaleOrderDto saleOrderDto) {
        SaleOrders saleOrders = new SaleOrders();
        BeanUtils.copyProperties(saleOrderDto, saleOrders);
        Double  totalAmount = 0.0;
        for(SaleOrderDetailDto saleOrderDetailDto:saleOrderDto.getSaleOrderDetailDtoList())
            totalAmount += saleOrderDetailDto.getSubtotal();
        saleOrders.setTotalAmount(totalAmount);
        saleOrderMapper.insert(saleOrders);

        SaleOrderDetails saleOrderDetails = new SaleOrderDetails();
        saleOrderDetails.setOrderId(saleOrders.getOrderId());
         for ( SaleOrderDetailDto saleOrderDetailDto:saleOrderDto.getSaleOrderDetailDtoList()) {
            BeanUtils.copyProperties(saleOrderDetailDto, saleOrderDetails);
            saleOrderDetailMapper.insert(saleOrderDetails);
         }
    }

    @Transactional
    @Override
    public void update(SaleOrderDto saleOrderDto) {
        SaleOrders saleOrders = new SaleOrders();
        BeanUtils.copyProperties(saleOrderDto, saleOrders);
        Double  totalAmount = 0.0;
        for ( SaleOrderDetailDto saleOrderDetailDto:saleOrderDto.getSaleOrderDetailDtoList())
            totalAmount += saleOrderDetailDto.getSubtotal();
        saleOrders.setTotalAmount(totalAmount);
        saleOrderMapper.update(saleOrders);

        saleOrderDetailMapper.deleteByOrderId(saleOrderDto.getOrderId());
        SaleOrderDetails saleOrderDetails = new SaleOrderDetails();
        saleOrderDetails.setOrderId(saleOrders.getOrderId());
        for ( SaleOrderDetailDto saleOrderDetailDto:saleOrderDto.getSaleOrderDetailDtoList()) {
            BeanUtils.copyProperties(saleOrderDetailDto, saleOrderDetails);
            saleOrderDetailMapper.insert(saleOrderDetails);
        }
    }

    @Override
    public void cancel(Long id) {
        SaleOrders  saleOrders = new SaleOrders();
        saleOrders.setStatus(StatusConstant.ROLE_ADMIN_USER);
        saleOrders.setOrderId(id);
        saleOrderMapper.update(saleOrders);
    }

    @Override
    public SaleOrderVo getById(Long id) {
        SaleOrders saleOrders = saleOrderMapper.getById(id);
        List<SaleOrderDetails> saleOrderDetails = saleOrderDetailMapper.getByOrderId(id);
        SaleOrderVo saleOrderVo = new SaleOrderVo();
        BeanUtils.copyProperties(saleOrders, saleOrderVo);
        saleOrderVo.setCustomerName(customerMapper.getById(saleOrders.getCustomerId()).getCustomerName());
        saleOrderVo.setUserName(userMapper.getById(saleOrders.getUserId()).getUsername());
        List<SaleOrderDetailVo>  saleOrderDetailVos = new ArrayList<>();
         for (SaleOrderDetails saleOrderDetail:saleOrderDetails){
            SaleOrderDetailVo saleOrderDetailVo = new SaleOrderDetailVo();
            BeanUtils.copyProperties(saleOrderDetail, saleOrderDetailVo);
            saleOrderDetailVo.setProductName(productMapper.getById(saleOrderDetail.getProductId()).getProductName());
            saleOrderDetailVos.add(saleOrderDetailVo);
        }
        saleOrderVo.setSaleOrderDetailVoList(saleOrderDetailVos);
        return saleOrderVo;
    }

    @Override
    public PageResult page(SaleOrderPageQueryDto saleOrderPageQueryDto) {
        PageHelper.startPage( saleOrderPageQueryDto.getPage(), saleOrderPageQueryDto.getPageSize());
        Page<SaleOrders> page = saleOrderMapper.page(saleOrderPageQueryDto);

        long total = page.getTotal();
        List <SaleOrders> records = page.getResult();
        List<SaleOrderWrapper> saleOrderWrappers = records.stream().map(record -> {
            String customerName = customerMapper.getById(record.getCustomerId()).getCustomerName();
            String userName = userMapper.getById(record.getUserId()).getUsername();
            return new SaleOrderWrapper(record, customerName, userName);
        }).collect(Collectors.toList());
        return new PageResult(total, saleOrderWrappers);
    }

    @Override
    public void updateStatus(Long id) {
        SaleOrders  saleOrders = new SaleOrders();
        saleOrders.setStatus(StatusConstant.ENABLE);
        saleOrders.setOrderId(id);
        saleOrderMapper.update(saleOrders);
    }

    @Override
    public void delivery(Long id) {
        SaleOrders  saleOrders = new SaleOrders();
        saleOrders.setStatus(StatusConstant.ROLE_USER);
        saleOrders.setOrderId(id);
        saleOrderMapper.update(saleOrders);
    }

    @Transactional
    @Override
    public void commit(StockOutOrderDto stockOutOrderDto) {
        SaleOrders  saleOrders = new SaleOrders();
        saleOrders.setStatus(StatusConstant.ROLE_SUPER_ADMIN);
        saleOrders.setOrderId(stockOutOrderDto.getOrderId());
        saleOrderMapper.update(saleOrders);

        StockOutOrders stockOutOrders = new StockOutOrders();
        BeanUtils.copyProperties(stockOutOrderDto, stockOutOrders);
        //1、订单类型为销售订单
        stockOutOrders.setRelatedOrderType(StatusConstant.DISABLE);
        //2、总金额
        Double  totalAmount = 0.0;
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList())
            totalAmount += stockOutOrderDetailsDto.getSubtotal();
        stockOutOrders.setTotalAmount(totalAmount);
        //3、相关订单id
        stockOutOrders.setRelatedOrderId(stockOutOrderDto.getOrderId());
        stockOutOrderMapper.insert(stockOutOrders);

        StockOutOrderDetails stockOutOrderDetails = new StockOutOrderDetails();
        stockOutOrderDetails.setOrderId(stockOutOrders.getOrderId());
        for(StockOutOrderDetailsDto stockOutOrderDetailsDto:stockOutOrderDto.getStockOutOrderDetailsDtoList()) {
            BeanUtils.copyProperties(stockOutOrderDetailsDto, stockOutOrderDetails);
            stockOutOrderDetailMapper.insert(stockOutOrderDetails);
        }
    }
}
